﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;

namespace UDS.Components
{
    public delegate int? FindItemDelegate(DataGridItem item);

    /// <summary>
    /// 常用工具函数类
    /// </summary>
    public class Tools
    {
        #region 将 DataReader 转为 DataTable

        /// <summary>
        /// 将DataReader 转为 DataTable
        /// </summary>
        /// <param name="dataReader">DataReader</param>
        public static DataTable ConvertDataReaderToDataTable(SqlDataReader dataReader)
        {
            using (dataReader)
            {
                var schemaTable = dataReader.GetSchemaTable();
                if (schemaTable == null)
                {
                    return null;
                }

                var datatable = new DataTable();
                int colColumnName = schemaTable.Columns.IndexOf("ColumnName");
                int colDataType = schemaTable.Columns.IndexOf("DataType");
                int colAllowNull = schemaTable.Columns.IndexOf("AllowDBNull");
                foreach (DataRow myRow in schemaTable.Rows)
                {
                    var myDataColumn = new DataColumn();
                    myDataColumn.DataType = (Type)myRow[colDataType];
                    myDataColumn.ColumnName = Convert.ToString(myRow[colColumnName]);

                    object obj = myRow[colAllowNull];
                    myDataColumn.AllowDBNull = (obj == null || (bool)obj);

                    datatable.Columns.Add(myDataColumn);
                }

                //添加数据
                int numColumns = schemaTable.Rows.Count;
                while (dataReader.Read())
                {
                    DataRow myDataRow = datatable.NewRow();
                    for (int i = 0; i < numColumns; i++)
                    {
                        myDataRow[i] = dataReader.IsDBNull(i) ? DBNull.Value : dataReader[i];
                        //myDataRow[i] = dataReader[i].ToString();
                    }
                    datatable.Rows.Add(myDataRow);
                }

                return datatable;
            }
        }

        #endregion

        #region 将英文的星期几转为中文

        public static string ConvertDayOfWeekToZh(DayOfWeek dw)
        {
            switch (dw)
            {
                case DayOfWeek.Sunday:
                    return "日";
                case DayOfWeek.Monday:
                    return "一";
                case DayOfWeek.Tuesday:
                    return "二";
                case DayOfWeek.Wednesday:
                    return "三";
                case DayOfWeek.Thursday:
                    return "四";
                case DayOfWeek.Friday:
                    return "五";
                case DayOfWeek.Saturday:
                    return "六";
            }

            return "";
        }

        #endregion

        #region FindSelectedItems

        public static IList<int> FindSelectedItems(DataGrid container, FindItemDelegate callback)
        {
            if (container == null)
            {
                throw new ArgumentNullException("container");
            }
            if (callback == null)
            {
                throw new ArgumentNullException("callback");
            }

            var list = new List<int>();
            foreach (DataGridItem item in container.Items)
            {
                int? value = callback(item);
                if (value != null)
                {
                    list.Add(value.Value);
                }
            }
            return list.AsReadOnly();
        }

        /// <summary>
        /// Find selected values from the specified <see cref="DataGrid"/>.
        /// If the specified checkbox is checked in a row, the data key will be added to the result list.
        /// </summary>
        public static IList<int> FindSelectedItems(DataGrid container, string checkBoxControlId)
        {
            if (container == null)
            {
                throw new ArgumentNullException("container");
            }
            if (string.IsNullOrEmpty(checkBoxControlId))
            {
                throw new ArgumentNullException("checkBoxControlId");
            }

            var list = new List<int>();
            foreach (DataGridItem item in container.Items)
            {
                var cb = (CheckBox)item.FindControl(checkBoxControlId);
                if (cb != null && cb.Checked)
                {
                    list.Add(int.Parse(container.DataKeys[item.ItemIndex].ToString()));
                }
            }
            return list.AsReadOnly();
        }

        #endregion FindSelectedItems
    }
}